package com.earnings.okhttputils.utils.ui;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.earnings.R;
import com.earnings.okhttputils.utils.bean.YData;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.ui.dialog.DetailDialog;
import com.earnings.okhttputils.utils.utils.SystemBarTintManager;
import com.zhy.autolayout.AutoLayoutActivity;

/**
 * Created by Administrator on 2017/5/5 0005.
 */

public abstract class YBaseActivity extends AutoLayoutActivity implements View.OnClickListener {
    protected TextView title_tv, top_right_tv;
    protected ViewGroup root_view;
    protected View top_view, top_xian_view;
    private DetailDialog detailDialog;
    protected ImageView top_img, back_view;
    protected Intent mIntent;
    protected Bundle bundleData;
    protected Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_layout);
        bundleData = getIntent().getExtras();
        if (bundleData == null) bundleData = new Bundle();
        mContext = this;
        baseInitView();
        init();

    }

    private void baseInitView() {
        root_view = (ViewGroup) findViewById(R.id.root_view);
        title_tv = (TextView) findViewById(R.id.title_tv);
        top_right_tv = (TextView) findViewById(R.id.top_right_tv);
        back_view = (ImageView) findViewById(R.id.back_view);
        top_img = (ImageView) findViewById(R.id.top_img);
        top_xian_view = findViewById(R.id.top_xian_view);
        top_view = findViewById(R.id.top_view);
        View view = getLayoutInflater().inflate(onCreateLayoutId(), null, false);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(layoutParams);
        root_view.addView(view);
        back_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        new HttpMap(getContext());

        top_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (detailDialog == null) {
                    detailDialog = new DetailDialog(YBaseActivity.this);
                }
                detailDialog.show();
            }
        });

    }

    protected void setTopViewBackgroundResource(int color) {
        top_view.setBackgroundColor(color);
    }

    protected void skipActivity(Class cls) {
        if (mIntent == null) mIntent = new Intent();
        mIntent.setClass(this, cls);
        mIntent.putExtras(bundleData);
        startActivity(mIntent);
    }


    protected void skipActivity(Class cls, int code) {
        if (mIntent == null) mIntent = new Intent();
        mIntent.setClass(this, cls);
        mIntent.putExtras(bundleData);
        startActivityForResult(mIntent, code);

    }


    protected void showRightTextView() {
        top_right_tv.setVisibility(View.VISIBLE);
    }

    protected void showRightTextView(String text) {
        top_right_tv.setText(text);
        top_right_tv.setVisibility(View.VISIBLE);
    }

    protected void showRightImage(int imgid) {

        top_img.setImageResource(imgid);
        top_img.setVisibility(View.VISIBLE);
    }

    protected void HideRightTextView() {
        top_right_tv.setVisibility(View.GONE);
    }

    protected void setHideTopView() {
        top_view.setVisibility(View.GONE);
    }

    protected void setShowTopView() {
        top_view.setVisibility(View.VISIBLE);
    }

    protected abstract int onCreateLayoutId();

    protected abstract void init();

    protected void setTitle(String title) {
        title_tv.setText(title);
    }

    /**
     * 更改状态栏颜色
     *
     * @param isSetColor 是否开启状态栏颜色变化
     */
    protected void setStateColodr(boolean isSetColor) {


        if (YData.app_type == YData.APP_ZCHB) {
            settingStatusBar(getResources().getColor(R.color.top_blue));
        }
        if (YData.app_type == YData.APP_BSSC) {
            settingStatusBar(getResources().getColor(R.color.top_rad));
        }

    }

    /**
     * 状态栏设置
     *
     * @param color
     */
    private void settingStatusBar(int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            setTranslucentStatus(true);
            //    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            // 激活状态栏设置
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(color);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            //After LOLLIPOP not translucent status bar
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //Then call setStatusBarColor.
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(color));
        }
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    public void onClick(View v) {

    }

    public Context getContext() {
        return this;
    }

    public int getIdFromXML(String name) {
        return getResources().getIdentifier(name, "id", getPackageName());
    }

    public int getDrawFromXML(String name) {
        return getResources().getIdentifier(name, "drawable", getPackageName());
    }

    public int getStringFromXML(String name) {
        return getResources().getIdentifier(name, "string", getPackageName());
    }

}
